#include <gl/glew.h>
#include <GL/glut.h>
#include "variables.h"

void initobjectnocol(GLuint object, GLfloat * vert, GLint sizevert, GLubyte * inds, GLint sizeind, GLenum type) {
	int offset = object * numperobj ; 
	glBindBuffer(GL_ARRAY_BUFFER, buffers[Vertices+offset]) ; 
	glBufferData(GL_ARRAY_BUFFER, sizevert, vert,GL_STATIC_DRAW);
	glVertexPointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0)) ; 
	glEnableClientState(GL_VERTEX_ARRAY) ; 
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[Elements+offset]) ; 
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeind, inds,GL_STATIC_DRAW);
	PrimType[object] = type ; 
	NumElems[object] = sizeind ; 
}

void drawobject(GLuint object) {
	int offset = object * numperobj ; 
	glBindBuffer(GL_ARRAY_BUFFER, buffers[Vertices+offset]) ; 
	glVertexPointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0)) ; 
	glEnableClientState(GL_VERTEX_ARRAY) ; 
	glBindBuffer(GL_ARRAY_BUFFER, buffers[Colors+offset]) ; 
	glColorPointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0)) ; 
	glEnableClientState(GL_COLOR_ARRAY) ; 
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[Elements+offset]) ; 
	glDrawElements(PrimType[object], NumElems[object], GL_UNSIGNED_BYTE, BUFFER_OFFSET(0)) ; 
}

void initcolorscube (void) {
	int base = numobjects * numperobj ; 
	for (int i = 0 ; i < ncolors ; i++) {
		for (int j = 0 ; j < 8 ; j++) 
			for (int k = 0 ; k < 3 ; k++) 
				cubecol[j][k] = _cubecol[i][k] ; 
		glBindBuffer(GL_ARRAY_BUFFER, buffers[base+i]) ; 
		glBufferData(GL_ARRAY_BUFFER, sizeof(cubecol), cubecol ,GL_STATIC_DRAW);
		glColorPointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0)) ; 
		glEnableClientState(GL_COLOR_ARRAY) ;       
	}  
}

void drawcolor(GLuint object, GLuint color) {
	int offset = object * numperobj ; 
	int base = numobjects * numperobj ; 
	glBindBuffer(GL_ARRAY_BUFFER, buffers[Vertices+offset]) ; 
	glVertexPointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0)) ; 
	glEnableClientState(GL_VERTEX_ARRAY) ; 
	glBindBuffer(GL_ARRAY_BUFFER, buffers[base+color]) ; // Set color correctly 
	glColorPointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0)) ; 
	glEnableClientState(GL_COLOR_ARRAY) ; 
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[Elements+offset]) ; 
	glDrawElements(PrimType[object], NumElems[object], GL_UNSIGNED_BYTE, BUFFER_OFFSET(0)) ; 
}